/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sistema.infraestrutura;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lauro
 */
public class ConnectionPool {

    private static Connection con = null;

    public static synchronized Connection getConnection() {
        if (con == null) {
            try {
                ComboPooledDataSource dataSource = new ComboPooledDataSource();
                dataSource.setDriverClass("com.mysql.jdbc.Driver");
                dataSource.setJdbcUrl(CFG.URL_BD);
                dataSource.setUser(CFG.USUARIO_BD);
                dataSource.setPassword(CFG.SENHA_BD);
                dataSource.setMinPoolSize(10);
                dataSource.setInitialPoolSize(5);
                dataSource.setMaxPoolSize(50);
                //dataSource.setAutomaticTestTable();
                dataSource.setCheckoutTimeout(60000);
                con = dataSource.getConnection();
                return con;
            } catch (SQLException ex) {
                Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
            } catch (PropertyVetoException ex) {
                Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return con;
    }
}
